package com.xls.box2023.home;

import com.xls.box2023.entity.YouXiEntity;
import com.xls.resource.dialog.utils.download.DownloadUtils;

import java.util.HashMap;

public class DownloadApkUtils {
    /**
    * 单例模式
    **/
    private static DownloadApkUtils mDownloadApkUtils = null;
        /**
         * 构造函数私有化
         **/
        private DownloadApkUtils() {
        }
        /**
         * 公有的静态函数，对外暴露获取单例对象的接口
         **/
        public static DownloadApkUtils getInstance() {
            if (mDownloadApkUtils == null) {
                synchronized (DownloadApkUtils.class) {
                    if (mDownloadApkUtils == null) {
                        mDownloadApkUtils = new DownloadApkUtils();
                    }
                }
            }
            return mDownloadApkUtils;
        }

    HashMap<Long,DownloadUtils> downloadUtils=new HashMap<>();//下载对象管理

    public void stopAllDownloadFile() {
        if (downloadUtils!=null&&downloadUtils.size()>0){
            for (Long appid:downloadUtils.keySet()){
                downloadUtils.get(appid).getObservable().dispose();
            }
        }
        downloadUtils.clear();
    }

    public void addDownLoad(long appId,  DownloadUtils down) {
        downloadUtils.put(appId,down);
    }

    public void remove(long id) {
        downloadUtils.remove(id);
    }

    public boolean isHaveDown(YouXiEntity youXiEntity) {
        if (downloadUtils!=null&&downloadUtils.size()>0){
            DownloadUtils downloadUtils1= downloadUtils.get(youXiEntity.getAppId());
            if (downloadUtils1!=null)return true;
        }
        return false;
    }
}
